﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Animation;
using Studio77.RuneMaster.Pages;
using Studio77.RuneMaster.Core;

namespace Studio77.RuneMaster
{
    public partial class RuneItem : UserControl
    {
        const double Show_Elimition_Delay = 700;

        public RuneItem()
        {
            InitializeComponent();
        }

        public int Type { get; set; }

        public bool IsRuneEnable { get; set; }

        /// <summary>
        /// 剩余的增加精神步数
        /// </summary>
        private int spriteAddLeftCount = 0;

        private Random r = new Random(DateTime.Now.Millisecond);

        private Storyboard storyEliminate;

        public int GetSpriteAddLeftCount()
        {
            return spriteAddLeftCount;
        }

        public void SetSpriteAddRune(int count)
        {
            spriteAddLeftCount = count;
            UpdateSpriteAdd();
        }

        public void SubSpriteAddLeftCount()
        {
            if (spriteAddLeftCount > 0)
            {
                spriteAddLeftCount--;
                UpdateSpriteAdd();
            }
        }

        public bool IsSpriteAddRune()
        {
            return spriteAddLeftCount > 0;
        }

        public void SetSpriteAddRune()
        {
            spriteAddLeftCount = 5;
            UpdateSpriteAdd();
            this.StoryboardSetSpriteRune.Begin();
        }

        /// <summary>
        /// 更新符文增加精神状态
        /// </summary>
        private void UpdateSpriteAdd()
        {
            if (spriteAddLeftCount <= 0)
            {
                SetType(Type);
            }
            else
            {
                SetAddSpriteType(Type);
            }
        }

        /// <summary>
        /// 消除符文
        /// </summary>
        /// <returns>返回消除时间</returns>
        public double Eliminate()
        {
            spriteAddLeftCount = 0;
            this.StoryboardImageShock.RepeatBehavior = RepeatBehavior.Forever;
            this.StoryboardImageShock.Begin();

            CompositeTransform trans = this.itemImage.RenderTransform as CompositeTransform;
            trans.ScaleX = 1;
            trans.ScaleY = 1;

            Duration duration = new Duration(TimeSpan.FromMilliseconds(Show_Elimition_Delay));
            //动画
            DoubleAnimation xAnimation = new DoubleAnimation
            {
                From = 1,
                To = 0,
                Duration = duration,
                EasingFunction = new QuinticEase()
            };
            DoubleAnimation yAnimation = new DoubleAnimation
            {
                From = 1,
                To = 0,
                Duration = duration,
                EasingFunction = new QuinticEase()
            };
            Storyboard.SetTargetProperty(xAnimation, new PropertyPath("ScaleX"));
            Storyboard.SetTargetProperty(yAnimation, new PropertyPath("ScaleY"));

            Storyboard storyboard = new Storyboard { };
            Storyboard.SetTarget(storyboard, itemImage.RenderTransform);
            storyboard.Children.Add(xAnimation);
            storyboard.Children.Add(yAnimation);
            storyboard.BeginTime = TimeSpan.FromMilliseconds(600);
            storyEliminate = storyboard;
            storyboard.Begin();

            this.IsRuneEnable = false;
            return Show_Elimition_Delay;
        }

        /// <summary>
        /// 选中
        /// </summary>
        public void Select()
        {
            StoryboardItemSelected.RepeatBehavior = RepeatBehavior.Forever;
            StoryboardItemSelected.Begin();
            imgSelector.Visibility = System.Windows.Visibility.Visible;
        }

        /// <summary>
        /// 取消选中
        /// </summary>
        public void UnSelect()
        {
            StoryboardItemSelected.Stop();
            imgSelector.Visibility = System.Windows.Visibility.Collapsed;
        }

        public void SetType(int type)
        {
            Canvas.SetZIndex(this, 1);
            string uri = string.Empty;
            string uri_outer = string.Empty;
            Type = type;
            IsRuneEnable = true;
            switch (type)
            {
                case 1:
                    uri = "/Images/blue.png";
                    uri_outer = "/Images/blue_outer_0.png";
                    break;
                case 2:
                    uri = "/Images/red.png";
                    uri_outer = "/Images/red_outer_0.png";
                    break;
                case 3:
                    uri = "/Images/yellow.png";
                    uri_outer = "/Images/yellow_outer_0.png";
                    break;
                case 4:
                    uri = "/Images/green.png";
                    uri_outer = "/Images/green_outer_0.png";
                    break;
                case 5:
                    uri = "/Images/purple.png";
                    uri_outer = "/Images/purple_outer_0.png";
                    break;
            }

            BitmapImage bitmap = new BitmapImage(new Uri(uri, UriKind.RelativeOrAbsolute));
            itemImage.Source = bitmap;

            BitmapImage bitmap2 = new BitmapImage(new Uri(uri_outer, UriKind.RelativeOrAbsolute));
            itemImage_outer.Source = bitmap2;

            this.StoryboardAddSpriteVanish.Stop();
            this.itemImage.Visibility = System.Windows.Visibility.Visible;
            this.itemImage_outer.Visibility = System.Windows.Visibility.Collapsed;
        }

        private void SetAddSpriteType(int type)
        {
            string uri = string.Empty;
            Type = type;
            IsRuneEnable = true;
            switch (type)
            {
                case 1:
                    uri = "/Images/blue_outer_" + spriteAddLeftCount + ".png";
                    break;
                case 2:
                    uri = "/Images/red_outer_" + spriteAddLeftCount + ".png";
                    break;
                case 3:
                    uri = "/Images/yellow_outer_" + spriteAddLeftCount + ".png";
                    break;
                case 4:
                    uri = "/Images/green_outer_" + spriteAddLeftCount + ".png";
                    break;
                case 5:
                    uri = "/Images/purple_outer_" + spriteAddLeftCount + ".png";
                    break;
            }

            BitmapImage bitmap = new BitmapImage(new Uri(uri, UriKind.RelativeOrAbsolute));
            itemImage.Source = bitmap;

            //如果spriteAddLeftCount =1，开始闪烁
            if (spriteAddLeftCount == 1)
            {
                this.StoryboardAddSpriteVanish.RepeatBehavior = RepeatBehavior.Forever;
                this.StoryboardAddSpriteVanish.Begin();
            }
        }

        internal void Show(int columnBlank)
        {
            spriteAddLeftCount = 0;
            if (this.storyEliminate != null)
            {
                this.storyEliminate.Stop();
            }

            this.StoryboardImageShock.Stop();
            this.itemImage.Visibility = System.Windows.Visibility.Visible;
            CompositeTransform trans = this.itemImage.RenderTransform as CompositeTransform;
            trans.ScaleX = 1;
            trans.ScaleY = 1;
            trans.TranslateY = GameStatics.ItemSize * columnBlank * -1;
            Duration duration = new Duration(TimeSpan.FromMilliseconds(columnBlank * GameStatics.RuneDownSpeed + 37));
            Duration duration2 = new Duration(TimeSpan.FromMilliseconds(1));
            //掉下来
            DoubleAnimation translateY = new DoubleAnimation
            {
                To = 0,
                Duration = duration,
            };
            DoubleAnimation xAnimation = new DoubleAnimation
            {
                From = 0.99,
                To = 1,
                Duration = duration2,
            };
            DoubleAnimation yAnimation = new DoubleAnimation
            {
                From = 0.99,
                To = 1,
                Duration = duration2,
            };

            //弹跳
            QuadraticEase bounce = new QuadraticEase();
            bounce.EasingMode = EasingMode.EaseIn;
            //bounce.Bounces = 2;
            //bounce.Bounciness = 5;
            translateY.EasingFunction = bounce;

            Storyboard.SetTargetProperty(translateY, new PropertyPath("TranslateY"));
            Storyboard.SetTargetProperty(xAnimation, new PropertyPath("ScaleX"));
            Storyboard.SetTargetProperty(yAnimation, new PropertyPath("ScaleY"));

            Storyboard storyboard = new Storyboard { };
            Storyboard.SetTarget(storyboard, itemImage.RenderTransform);
            storyboard.Children.Add(yAnimation);
            storyboard.Children.Add(xAnimation);
            storyboard.Children.Add(translateY);
            storyboard.Begin();

        }

        private int addSpriteBase = 1;

        public void FlyAddSpriteFlag(int addMulti)
        {
            SoundManager.PlayEffect(SoundManager.addSpirit);

            addSpriteBase = addMulti;
            string uri = "/Images/add_sprite_flag_" + addMulti + ".png";
            BitmapImage bitmap = new BitmapImage(new Uri(uri, UriKind.RelativeOrAbsolute));
            imgAddSpriteFlag.Source = bitmap;

            this.imgAddSpriteFlag.Visibility = System.Windows.Visibility.Visible;
            double top = Canvas.GetTop(this);
            double left = Canvas.GetLeft(this);

            double aimX = -left - GameStatics.ItemSize / 2;
            double aimY = -top - GameStatics.ItemSize / 2 - 70;

            Duration duration = new Duration(TimeSpan.FromMilliseconds(650));
            //移动
            DoubleAnimation translateY = new DoubleAnimation
            {
                From = 0,
                To = aimY,
                Duration = duration,
            };
            DoubleAnimation translateX = new DoubleAnimation
            {
                From = 0,
                To = aimX,
                Duration = duration,
            };
            DoubleAnimation xAnimation = new DoubleAnimation
            {
                From = 1,
                To = 1.2,
                Duration = duration,
            };
            DoubleAnimation yAnimation = new DoubleAnimation
            {
                From = 1,
                To = 1.2,
                Duration = duration,
            };


            QuadraticEase bounce = new QuadraticEase();
            bounce.EasingMode = EasingMode.EaseOut;
            translateY.EasingFunction = bounce;
            translateX.EasingFunction = bounce;

            Storyboard.SetTargetProperty(translateY, new PropertyPath("TranslateY"));
            Storyboard.SetTargetProperty(translateX, new PropertyPath("TranslateX"));
            Storyboard.SetTargetProperty(xAnimation, new PropertyPath("ScaleX"));
            Storyboard.SetTargetProperty(yAnimation, new PropertyPath("ScaleY"));

            Storyboard storyboard = new Storyboard { };
            Storyboard.SetTarget(storyboard, imgAddSpriteFlag.RenderTransform);
            storyboard.Children.Add(yAnimation);
            storyboard.Children.Add(xAnimation);
            storyboard.Children.Add(translateX);
            storyboard.Children.Add(translateY);
            storyboard.Completed += storyboard_Completed;
            storyboard.Begin();


        }

        void storyboard_Completed(object sender, EventArgs e)
        {
            this.imgAddSpriteFlag.Visibility = System.Windows.Visibility.Collapsed;

            this.spriteAddLeftCount = 0;
            UpdateSpriteAdd();

            //增加精神力数量
            GamePage.Instance.AddSprite(addSpriteBase);
        }
    }
}
